<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Items extends CI_Controller {

	function  mainShow(){
		
		if($this->session->userdata("msg")){
			$data['msg']=$this->session->userdata("msg");
		}
		if($this->session->userdata("user_id")){
			$data['user_id']=$this->session->userdata("user_id");			
		}

		$sql = "SELECT * FROM items order by like_count desc ,creat_time desc";
		$data['items']=$this->db->query($sql);
		$this->load->view('main.php',$data);
	}

	function  addItem(){
		$url=$_POST["url"];
		$shopCode='';
		$item_id='';
			
		if(preg_match("/http:\/\/.*?\/([^\/]+)\//i",$url,$matches)){
			$shopCode = $matches[1];
		}else{
			echo "get shopCode error";
		}
			
		$curl=instance::getCurl();
		$data =$curl->getFormRakuten($url);

		if(preg_match("/<input value=\"([0-9]+)\" type=\"\w*\" name=\"item_id\">/iUs",$data,$matches1)){
			$item_id = $matches1[1];
		}else{
			$this->session->set_userdata('msg_url', "The URL is not right");
			redirect('items/myPageShow');
		}

		$itemCode=$shopCode.':'.$item_id;

		//get imformation by itemcode search API
		$url = 'http://api.rakuten.co.jp/rws/3.0/json?';
		$developerId='d8907e15edf85f0f302dfbb08ee8d5e9';
		$affiliateId='0eb14fc9.51366477.0eb14fca.155c4d47';
		$operation='ItemCodeSearch';
		$version='2010-08-05';

		$url = $url.'developerId='.$developerId.'&affiliateId='.$affiliateId.'&operation='.$operation.'&version='.$version.'&itemCode='.$itemCode;
			
		$content = $curl->getFormRakuten($url);
		$Items_Array = json_decode($content,true);
			
		$item_arry= $Items_Array["Body"]["ItemCodeSearch"]["Items"]["Item"][0];
			
		$item["mediumImageUrl"]=$item_arry["mediumImageUrl"];
		$item["affiliateUrl"]=$item_arry["affiliateUrl"];
		$item["itemPrice"]=$item_arry["itemPrice"];
		$item["itemCode"]=$itemCode;
			
		//--------------get the genreId----------------//
		$url='http://api.rakuten.co.jp/rws/3.0/json?developerId=d8907e15edf85f0f302dfbb08ee8d5e9&affiliateId=0eb14fc9.51366477.0eb14fca.155c4d47&operation=GenreSearch&version=2007-04-11&';
		$genreId=$item_arry["genreId"];

		$url=$url.'genreId='.$genreId;
			
		$content = $curl->getFormRakuten($url);
		$GenreL_Array = json_decode($content,true);
			
		$GenreSearch_Array=$GenreL_Array["Body"]["GenreSearch"];
		$Current_Array=$GenreSearch_Array["current"][0];
		$Parent_Array=$GenreSearch_Array["parent"];
		$genreLevel= $GenreSearch_Array["current"][0]["genreLevel"];
		$genreId=$GenreSearch_Array["current"][0]["genreId"];
			
		if($genreLevel!='1'){
			foreach($Parent_Array as $value){
				if($value["genreLevel"]=='1'){
					$genreId=$value["genreId"];
				}
			}
		}
			
		$item["genreId"]=$genreId;
			
		//--------------end of get genreId--------------------
		if($this->session->userdata("user_id")){
			$item['user_id']=$this->session->userdata("user_id");
		}else{
			$this->session->set_userdata('msg', "login first");
			redirect('items/mainShow/');
		}

		$this->load->view('items/upLoad.php',$item);
	}

	function items_insert(){
		//htmlspecialchars(string,quotestyle,character-set);
		
		if($this->session->userdata("user_id")){
			$data['user_id']=$this->session->userdata("user_id");
			$_POST["user_id"]=$data['user_id'];
			$_POST["description"]=htmlspecialchars($_POST["description"],ENT_COMPAT,UTF-8);

			$this->db->insert('items',$_POST);
			redirect('items/myPageShow/');
		}else{
			$this->session->set_userdata("msg","please login");
			redirect('welcome');
		}
		
		
	}

	function getItem(){
		if($this->session->userdata("msg")){
			$data['msg']=$this->session->userdata("msg");
		}
		if($this->session->userdata("user_id")){
			$data['user_id']=$this->session->userdata("user_id");			
		}
		
		$para["item_id"]=$this->uri->segment(3);
		$para["pastUrl"]=$this->uri->segment(4);
		$sql = "SELECT * FROM items WHERE item_id = ? ";
		$data['items']=$this->db->query($sql,$para);
		$data["pastUrl"]=$this->uri->segment(4);
		

		$this->load->view('items/itemShow.php',$data);

	}


	function myPageShow(){
		
		$sql = "SELECT * FROM items WHERE user_id = ? order by creat_time desc";
		if($this->session->userdata("user_id")){
			$data['user_id']=$this->session->userdata("user_id");
			$data['items']=$this->db->query($sql,$data['user_id']);
			if($this->session->userdata("msg_url")){
				$data['msg_url']=$this->session->userdata("msg_url");
			}
			$this->load->view('items/myPage.php',$data);
		}else{
			$this->session->set_userdata('msg', "login first");
			redirect('items/mainShow/');
		}
	}

	function getByGenre(){
		$GenreId = array("0","101240","100804","101164","100533","215783","216129","101070","100938","100316","100026",
						"216131","100371","100005","101213","211742","100227","100939","101381",
						"200163","101438","100000","402853","503190","100433","510901","510915","551167","551169",
						"551177","558885","558929","558944","101114","200162");
		
		if($this->session->userdata("msg")){
			$data['msg']=$this->session->userdata("msg");
		}
		if($this->session->userdata("user_id")){
			$data['user_id']=$this->session->userdata("user_id");			
		}

		$sql = "SELECT * FROM items where genre_id= ? order by like_count desc ,creat_time desc";
		$para["genre_id"] = $GenreId[$_POST["num"]];
		$data['items']=$this->db->query($sql,$para);

		$this->load->view('main.php',$data);
		
	}
}

class instance{
	private static $a;
	static function getCurl(){
		if(!self::$a){
			self::$a = new curl();
		}
		return self::$a;
	}
}

class curl{
	function getFormRakuten($url){
			
		$ch = curl_init($url);

		//curl_setopt($ch, CURLOPT_URL, $url);

		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
			
		curl_setopt($ch, CURLOPT_HTTPAUTH,CURLAUTH_NTLM);
			
		curl_setopt($ch, CURLOPT_PROXY, 'http://proxysv.intra.rakuten.co.jp:80');
			
		curl_setopt($ch, CURLOPT_PROXYPORT, '80');
			
		curl_setopt($ch, CURLOPT_PROXYUSERPWD, "ar-chengjun.zhang:Nr0097xv");

		$data = curl_exec($ch);

		curl_close($ch);

		return $data;
	}
}


